<?php
//***************  add by code   ******************
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of article
 *
 * @author Administrator
 */
class search extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->url = $this->uri->segments;
        $this->CI = & get_instance();
    }

    public function index() {
        $data['action_info'] = $this->m_public->getRowInfo('class',array('classpropertyid'=>23,'configid'=>CONFIG_ID));

        $post = $this->input->post();
        $search_content = trim($post['SearchKey']);
        $search_content = $this->lib_replace_end_tag($search_content);

        $result = array();

        $condition = array(
            'from' => 'product',
            'where' => array(
                'like' => array(
                    'column' => 'productname',
                    'content' => $search_content,
                    'type' => 'both'
                )
            )
        );
        $products = $this->m_public->get_list($condition);
        if(!empty($products)){
            foreach($products as $key=>$val){
                $products[$key]['type'] = 'product';
                $result[] = $products[$key];
            }
        }

        $condition = array(
            'from' => 'article',
            'where' => 'title like "%'.$search_content.'%" or details like "%'.$search_content.'%"',
        );
        $articles = $this->m_public->get_list($condition);
        if(!empty($articles)){
            foreach($articles as $key=>$val){
                $articles[$key]['type'] = 'article';
                $result[] = $articles[$key];
            }
        }
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        /*构造分页*/
        $total_row = count($result);
        $page_size = 10;
        $total_page = ceil($total_row / $page_size);
        $start_row = ($page - 1) * $page_size;
        $page_str = '<div class="page" style="margin-left: 314px;">';
        if($page != 1){
            $page_str .= '<a class="pa_up" href="/cn/search.html?&page='.($page-1).'">上一页</a>';
        }
        for($i=1;$i<=$total_page;$i++){
            if($i==$page){
                $page_str .= '<a class="selected">'.$i.'</a>';
            }else{
                $page_str .= '<a href="/cn/search.html?page='.$i.'">'.$i.'</a>';
            }
        }
        if($page != $total_page){
            $page_str .= '<a class="pa_next" href="/cn/search.html?page='.($page+1).'">下一页</a>';
        }
        $page_str .= '</div>';
        $data['result'] = $result;
        $data['start_key'] = $start_row;
        $data['end_key'] = $start_row + $page_size;
        $data['page_str'] = $page_str;

        /*右侧部分，联系我们*/
        $contact_config = $this->m_public->getRowInfo('config',array('configid'=>CONFIG_ID),'contact_config');
        $data['contact'] = json_decode($contact_config['contact_config'],true);

        /*头部,底部数据*/
        $config = $this->m_public->getRowInfo('config',array('configid'=>CONFIG_ID),'copyright,banquan,contact_config,footerinfo');
        $config['contact_config'] = json_decode($config['contact_config'],true);
        $data['config'] = $config;
        $footer_condition = array(
            'from' => 'link',
            'where' => array('isclose'=>0,'typeid'=>1,'configid'=>CONFIG_ID),
            'order' => 'listid'
        );
        $data['footer_info'] = $this->m_public->get_list($footer_condition);

        $this->load->view('search',$data);
    }

    private function lib_replace_end_tag($str){
        if (empty($str)) return false;
        $str = htmlspecialchars($str);
        $str = str_replace( '/', "", $str);
        $str = str_replace("\\", "", $str);
        $str = str_replace(">", "", $str);
        $str = str_replace("<", "", $str);
        $str = str_replace("<SCRIPT>", "", $str);
        $str = str_replace("</SCRIPT>", "", $str);
        $str = str_replace("<script>", "", $str);
        $str = str_replace("</script>", "", $str);
        $str=str_replace("select","select",$str);
        $str=str_replace("join","join",$str);
        $str=str_replace("union","union",$str);
        $str=str_replace("where","where",$str);
        $str=str_replace("insert","insert",$str);
        $str=str_replace("delete","delete",$str);
        $str=str_replace("update","update",$str);
        $str=str_replace("like","like",$str);
        $str=str_replace("drop","drop",$str);
        $str=str_replace("create","create",$str);
        $str=str_replace("modify","modify",$str);
        $str=str_replace("rename","rename",$str);
        $str=str_replace("alter","alter",$str);
        $str=str_replace("cas","cast",$str);
        $str=str_replace("&","&",$str);
        $str=str_replace(">",">",$str);
        $str=str_replace("<","<",$str);
        $str=str_replace(" ",chr(32),$str);
        $str=str_replace(" ",chr(9),$str);
        $str=str_replace("    ",chr(9),$str);
        $str=str_replace("&",chr(34),$str);
        $str=str_replace("'",chr(39),$str);
        $str=str_replace("<br />",chr(13),$str);
        $str=str_replace("''","'",$str);
        $str=str_replace("css","'",$str);
        $str=str_replace("CSS","'",$str);
        return $str;
    }
}
